package com.example.zcq.rxjava2retrofitmvp.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.zcq.rxjava2retrofitmvp.R;
import com.example.zcq.rxjava2retrofitmvp.base.BaseActivity;
import com.example.zcq.rxjava2retrofitmvp.constant.Constant;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.util.List;
/**
* @package com.example.zcq.rxjava2retrofitmvp.activity
* @fileName ShowPicActivity
* @description: 从相册选择图片和拍照  拍照部分机型崩溃  比如360n5s
* @author zcq
* @date 2018/9/27 13:12
* @org zcq有限公司
* @email 1015857694@qq.com
*/

public class ShowPicActivity extends BaseActivity implements View.OnClickListener {

    /**
     * 跳转到Rxjava2activity
     */
    private Button mBtn0;
    /**
     * 跳转到Rxjava2activity
     */
    private Button mBtn1;
    /**
     * 选择图片
     */
    private Button mBtn2;
    private ImageView mIv0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_pic);
        initView();

        //        selectePic();
    }

    //选择图片
    private void selectePic() {
        Matisse
                .from(ShowPicActivity.this)

                                .choose(MimeType.allOf())//照片视频全部显示
//                .choose(MimeType.of(MimeType.JPEG, MimeType.PNG, MimeType.GIF))

                .countable(true)//有序选择图片

                .maxSelectable(9)//最大选择数量为9
                .capture(true) //是否提供拍照功能
                .captureStrategy(new CaptureStrategy(true, "com.example.zcq.rxjava2retrofitmvp"))//存储到哪里
                .gridExpectedSize(120)//图片显示表格的大小getResources()
                //                .gridExpectedSize(
                //                        getResources().getDimensionPixelSize(R.dimen.grid_expected_size))//等同上面的
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)//图像选择和预览活动所需的方向。
                .thumbnailScale(0.85f)//缩放比例
                .theme(R.style.Matisse_Zhihu)//主题  暗色主题 R.style.Matisse_Dracula
                .imageEngine(new GlideEngine())//加载方式
                .forResult(Constant.REQUEST_CODE_CHOOSE);//请求码


    }


    private void initView() {
        mBtn0 = (Button) findViewById(R.id.btn0);
        mBtn0.setOnClickListener(this);
        mBtn1 = (Button) findViewById(R.id.btn1);
        mBtn1.setOnClickListener(this);

        mIv0 = (ImageView) findViewById(R.id.iv0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn0:
                selectePic();
                break;
            case R.id.btn1:
                break;
        }
    }


    List<Uri> mSelected;

    @Override      //接收返回的地址
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
            mSelected = Matisse.obtainResult(data);
            mBtn1.setText("mSelected: " + mSelected);
            Log.d("Matisse", "mSelected: " + mSelected);
            Glide.with(this).load(mSelected.get(0)).into(mIv0);
        }
    }

}
